<template>
  <!-- -->
  <div class="ethernet-settings">
    <div class="top-box">
      <EthernetSettingsTable style="min-width: 400px; max-width: 800px" ref="eTable" @save="handleSave"/>
    </div>
    <div class="bottom-box">
      <v-btn @click="refresh" class="refresh-btn" style="background-color: #003366; color: white; margin-right: 10px;">
        刷新
      </v-btn>
      <v-btn class="save-btn" @click="save" style="background-color: #FFA500; color: white;">保存</v-btn>
    </div>
  </div>
</template>

<script>
import EthernetSettingsTable from "@/components/EthernetSettingsTable.vue";

export default {
  name: 'EthernetSettingsContent',
  data() {
    return {
    };
  },
  methods: {
    refresh() {
      // 刷新逻辑
      localStorage.setItem('curItem', '端口映射');
      this.$emit('refresh')
    },
    save() {
      this.$refs.eTable.returnData();
    },
    handleSave(rows) {
      // console.log(rows);
      const formatted = {
        'cmd': 244,
        'config': {
          'portmap': []
        }
      }
      for (const r in rows) {
        if (rows[r].confirmed) {
          const line = {
            'ipaddr': rows[r].remoteIp,
            'port': parseInt(rows[r].remotePort)
          }
          formatted.config.portmap.push(line);
        }
      }
      const data = JSON.stringify(formatted);
      this.$emit('sendData', data);
    }
  },
  components: {
    EthernetSettingsTable
  }
};
</script>

<style scoped>
.ethernet-settings {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  height: 100%;
  padding-top: 10vh;
}

.top-box {
  width: 100%;
  box-sizing: border-box;
}

.bottom-box {
  display: flex;
  justify-content: center;
  padding: 20px;
}

.refresh-btn {
  background-color: #003366;
  color: white;
  margin-right: 10px;


}

.save-btn {
  background-color: #FFA500;
  color: white;
}
</style>
